package org.coderfun.boot.web.controller;



import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import klg.common.model.EasyUIPage;
import klg.common.model.JsonData;
import klg.common.utils.MyPrinter;
import klg.query.jpa.expr.AExpr;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.coderfun.boot.core.entity.User;
import org.coderfun.boot.core.entity.User_;
import org.coderfun.boot.core.service.UserService;
import org.coderfun.common.log.LogModuleCode;
import org.coderfun.common.log.Logger;



/**
 *
 * 
 * Generated by fieldmeta at 2018-09-20T21:15:17+08:00
 *
 */

@Controller("adminUserController")
@RequestMapping("/admin/action/boot/user")
public class UserController {
	@Autowired
	UserService userService;
	
	@ResponseBody
	@PostMapping("/add")
	@RequiresPermissions("boot:user:add")
	public JsonData add(
			@ModelAttribute User user){
		
		userService.create(user);
		return JsonData.success();
	}
	
	@Logger(name = "修改用户" , moduleCode = LogModuleCode.BOOT)
	@ResponseBody
	@PostMapping("/edit")
	@RequiresPermissions("boot:user:edit")
	public JsonData edit(
			@ModelAttribute User user){
				
		userService.update(user,"loginName", "password", "salt");
		return JsonData.success();
	}
	
	@Logger(name = "修改用户密码" , moduleCode = LogModuleCode.BOOT)
	@ResponseBody
	@PostMapping("/updatePassword")
	@RequiresPermissions("boot:user:updatePassword")
	public JsonData updatePassword(
			@RequestParam Long userId,@RequestParam String plainPassword){
		
		userService.updatePassword(userId, plainPassword);
		return JsonData.success();
	}
	
	@ResponseBody
	@RequestMapping("/checkLoginName")
	@RequiresPermissions("boot:user:query")
	public boolean checkLoginName(@RequestParam String loginName){
		return userService.checkLoginNameExist(loginName);
	}
	
	@ResponseBody
	@RequestMapping("/findpage")
	@RequiresPermissions(value = {"boot:user:query","boot:userRole:query"} ,logical =Logical.OR)
	public EasyUIPage findpage(
			@ModelAttribute User user,
			@RequestParam int page,
			@RequestParam int rows){
		Pageable pageable=new PageRequest(page<1?0:page-1, rows, new Sort(Direction.DESC,"id"));
		Page<User> pageData=userService.findPage(pageable,
				AExpr.contain(User_.loginName, user.getLoginName()).igEmpty(),
				AExpr.contain(User_.name, user.getName()).igEmpty(),
				AExpr.contain(User_.phone, user.getPhone()).igEmpty(),
				AExpr.eq(User_.state, user.getState()).igEmpty());
		return new EasyUIPage(pageData);
	}
	
	@ResponseBody
	@RequestMapping("/findlist")
	@RequiresPermissions(value = {"boot:user:query","boot:userRole:query"} ,logical =Logical.OR)
	public JsonData findlist(
			@ModelAttribute User user){
		
		List<User> listData=userService.findList(user, new Sort(Direction.DESC,"id"));
		return JsonData.success(listData);
	}	
}
